<?php
/**
 * @Created on 2021/05/25
 * @Author Qi
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Project extends Base{

    //推广项目管理
    public function index(){

        $where = array();
        $name=request()->param('name');
        if($name){
            $where['name'] = array('like',"%{$name}%");
        }
        $cus=request()->param('cus');
        if($cus){
            $where['cus_id'] = $cus;
        }
        $ader=request()->param('ader');
        if($ader){
            $where['ader_id'] = $ader;
        }
        $acc=request()->param('acc');
        if($acc){
            $where['accaid'] = array('like',"%{$acc}%");
        }
        $start=request()->param('start');
        if($start){
            $where['starttime'] = array('egt',strtotime($start));
        }
        $end=request()->param('end');
        if($end){
            $where['starttime'] = array('elt',strtotime($end));
        }
        if(!empty($start) && !empty($end)){
            $where['starttime'] = array('between time',["{$start}","{$end}"]);
        }
        //筛选下拉
        $aderlist=db('ader')->field('id,ader')->order('id desc')->select();
        $cuslist=db('customer')->field('id,name')->order('id desc')->select();
        $list=db('project')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('name',$name);
        $this->assign('cus',$cus);
        $this->assign('ader',$ader);
        $this->assign('acc',$acc);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('aderlist',$aderlist);
        $this->assign('cuslist',$cuslist);
        return $this->fetch();
    }

    //添加
    public function add(){

        if(request()->isPost()){
            $name = trim(request()->param('name'));
            $cus_id = trim(request()->param('cus_id'));
            $accid = trim(request()->param('accid'));
            $rebate = trim(request()->param('rebate'));
            $starttime = trim(request()->param('starttime'));
            $status = trim(request()->param('status'));
            if($accid){
                $accinfo=db('account')->field('a_id,name,ader_id')->where('id',$accid)->find();
                $data['accname'] = $accinfo['name'];
                $data['accaid'] = $accinfo['a_id'];
                $data['ader_id'] = $accinfo['ader_id'];
            }
            $data['name'] = $name;
            $data['cus_id'] = $cus_id;
            $data['accid'] = $accid;
            $data['rebate'] = $rebate;
            $data['starttime'] = strtotime($starttime);
            $data['status'] = $status;
            $data['add_time'] = time();
            $true=db('project')->insert($data);
            if($true){
                system_log('project','添加推广项目');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            $aderlist=db('ader')->field('id,ader')->order('id desc')->select();
            $cuslist=db('customer')->field('id,name')->order('id desc')->select();
            $acclist=db('account')->field('id,name')->where('status',1)->order('id desc')->select();
            $this->assign('aderlist',$aderlist);
            $this->assign('cuslist',$cuslist);
            $this->assign('acclist',$acclist);
            return $this->fetch();
        }
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $id = request()->param('id');
            $name = trim(request()->param('name'));
            $cus_id = trim(request()->param('cus_id'));
            $accid = trim(request()->param('accid'));
            $rebate = trim(request()->param('rebate'));
            $starttime = trim(request()->param('starttime'));
            $status = trim(request()->param('status'));
            if($accid){
                $accinfo=db('account')->field('a_id,name,ader_id')->where('id',$accid)->find();
                $data['accname'] = $accinfo['name'];
                $data['accaid'] = $accinfo['a_id'];
                $data['ader_id'] = $accinfo['ader_id'];
            }
            $data['name'] = $name;
            $data['cus_id'] = $cus_id;
            $data['accid'] = $accid;
            $data['rebate'] = $rebate;
            $data['starttime'] = strtotime($starttime);
            $data['status'] = $status;
            $true=db('project')->where('id',$id)->update($data);
            if($true){
                system_log('project','修改推广项目');
                return json(['status'=>1,'message'=>'修改成功']);
            }else{
                return json(['status'=>0,'message'=>'修改失败']);
            }
        }else{
            $id=request()->param('id');
            $info=db('project')->where('id',$id)->find();
            $this->assign('info',$info);
            $aderlist=db('ader')->field('id,ader')->order('id desc')->select();
            $cuslist=db('customer')->field('id,name')->order('id desc')->select();
            $acclist=db('account')->field('id,name')->where('status',1)->order('id desc')->select();
            $this->assign('aderlist',$aderlist);
            $this->assign('cuslist',$cuslist);
            $this->assign('acclist',$acclist);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        $id=request()->param('id');
        $true=db('project')->where('id',$id)->delete();
        if($true){
            system_log('project','删除推广项目');
            return json(['status'=>1,'message'=>'删除成功']);
        }else{
            return json(['status'=>0,'message'=>'删除失败']);
        }
    }

}